<?php
/**
 * Created by PhpStorm.
 * Service: Administrator
 * Date: 2018/6/6/006
 * Time: 9:32
 */

namespace App\Http\Controllers\Product;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Service\Product\ProductService;
use App\Http\Controllers\Controller;
//use Illuminate\Support\Facades\Redis;
use Session,Config;
use Cache;

class IndexController extends Controller
{
    /**
     * @return view
     * Demo页面
     */
    public function index()
    {
        return view('Product.setting');
    }

    /**
     * @return view
     * Demo页面
     */
    public function productList()
    {
        $where = [];

        $Product = new ProductService();

        $data =  $Product->getTypeList($where);

        return view('Product.productlist')->with('data',$data['data']);
    }
    /**
     * @param Request $request
     * 添加套餐类型
     */
    public function addProductType(Request $request)
    {

        $where = $request->except('_token');

        $Product = new ProductService();

        $ret =  $Product->addProductType($where);

        if($ret)
        {
            return $ret;
        }

    }


    /**
     * @param Request $request
     * 添加套餐时间类型
     */
    public function addProductTimeType(Request $request)
    {

        $where = $request->except('_token');

        $Product = new ProductService();

        $ret =  $Product->addProductTimeType($where);
//        dd($ret);

        if($ret)
        {
            return $ret;
        }

    }

    /**
     * @param Request $request
     * 添加产品
     */
    public function addProduct(Request $request)
    {

        $where = $request->except('_token');

        $Product = new ProductService();

        $ret =  $Product->addProduct($where);

        if($ret)
        {
            return $ret;
        }

    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 删除账号
     */
    public function delProduct(Request $request)
    {

        $where = [];
        $where['product_id']=trim($request->input('product_id'),',');


        $user = new ProductService();
        $data = $user->delProduct($where);

        if($request->ajax())
        {
            return response()->json($data);
        }else{

            if($data['error_code'] == '0')
            {
                echo '<script>location.href="/productList";alert("删除成功！");</script>';
            }else{
                echo "<script>confirm('{$data['error_message']}')?location.href='/productList' : history.back() ;</script>";
                exit();
            }
        }

    }

    /**
     * @param Request $request
     * 修改产品信息
     */
    public function updateProduct(Request $request)
    {
        $where = [];
        $where= $request->except('_token');
//        $reg = "/[[:punct:]]/i";
//        $where['remark'] = preg_replace($reg, '', $where['remark']);
//        dd($where);
        $Product = new ProductService();

        $data = $Product->updateProduct($where);

        if($data)
        {
           return $data;
        }
    }

    /**
     * @param Request $request
     * 产品账号列表
     */
    public function getProductList(Request $request)
    {

        $where= $request->except('_token');

        if($request->has('page'))
        {
            $where['page_no'] = $request->input('page');
            unset($where['page']);
        }
        if($request->has('limit'))
        {
            $where['page_size'] = $request->input('limit');
            unset($where['limit']);
        }else{
            $where['page_size'] = 10;
        }

        foreach($where as $key=>$value)
        {
            if($value =='' || $value == 'undefined')
            {
                unset($where[$key]);
            }
        }


        $Product = new ProductService();

        $data = $Product->getProductList($where);

        if($data)
        {
            foreach ($data['data'] as &$v)
            {
                $v['status_name'] = $v['status'] == 1 ? '上线':'下线';
                $v['type_id_name'] = $this->getTypeName($v['type_id'],'product_type')['set_meal_name'];
                $v['time_type_name'] = $this->getTypeName($v['time_type'],'time_type')['time_name'];
            }
        }

        return $data;

    }

    private function getTypeName($type_id,$type)
    {
        $where = [];
        $Product = new ProductService();

        $data =  $Product->getTypeList($where);

        foreach ($data['data'][$type] as $v){
            if($v['id'] == $type_id){
                return $v;
            }
        }

    }





}